<?php
require('../database/db.php');
$mysql=new mysql();
error_reporting(0);
// 分页
if($_REQUEST['page']!="" && $_REQUEST['pagesize']!=""){
	$de=$_REQUEST['page']*$_REQUEST['pagesize'];
	$size=$_REQUEST['pagesize'];
}else{
	$de=0;
	$size=6;
}
// 分页 end
// 需传参数
$cateid=$_REQUEST['cateid'];
$activity_id=$_REQUEST['activity_id'];
// 需传参数 end

if ($cateid!='' && $activity_id!='') {
	//cateid 为 0 ，则获取全部分类商品
	if($cateid==0){
		//根据活动拿到厂商分类（商品第一级分类）
		$query = mysql_query("select * from h_activity_fenl_tu  where activity_id=".$_REQUEST[activity_id]." order by cateid");
		$h_activity_fenl_tu = array();
		while ($row = mysql_fetch_assoc($query)){
			$h_activity_fenl_tu[] = $row['cateid'];
		}
		$h_activity_fenl = implode(',',$h_activity_fenl_tu);

	    if($h_activity_fenl == ""){
	    	$arr = array('code' => 300, 'msg' =>'没有商品');
			echo json_encode($arr);
			exit();
	    }
	}else{
		$h_activity_fenl = $cateid;
	}
    //根据厂商分类（商品第一级分类）拿到商品第二级分类
    $zileimuArray = array();
	$query = mysql_query("select * from zifenlei where fenleiid in (".$h_activity_fenl.")");
	while ($row = mysql_fetch_assoc($query)) {
		$zileimuArray[] = $row['id'];
	}
	$zileimu=implode(',',$zileimuArray);
    if($zileimu==""){
    	$arr = array('code' => 300, 'msg' =>'没有商品');
		echo json_encode($arr);
		exit();
    }
    
    //1 为扫货，2 为限时折扣， 3 首选精品，4 排行榜 ，5 你想要的，6 天天上新
    if($activity_id==4){
    	if($cateid == 0){
             //$sql = "SELECT * FROM goods  WHERE  zixilie IN (".$zileimu.") order by xiaoliang  desc  limit $de,$size";
             $sql = "SELECT * FROM goods WHERE  zixilie IN (".$zileimu.")  limit $de,$size";
    	}else{
             //$sql = "SELECT * FROM goods  WHERE  zixilie IN (".$zileimu.")  and goods.check=1 order by xiaoliang  desc  limit $de,$size";
             $sql = "SELECT * FROM goods  WHERE  zixilie IN (".$zileimu.")   limit $de,$size";
    	}
    }else if($activity_id==6){
    	$startime=date('Y-m-d',time());
    	$endtime=date('Y-m-d',time()-3600*24*7);
        //根据厂商分类（商品第一级分类）拿到对应厂商
        //待过滤的厂商
        $shopidsArray = explode(',', $_REQUEST['shopids']);
	    $shopArray = array();
		$query = mysql_query("select * from shop where leimu in (".$h_activity_fenl.")");
		while ($row = mysql_fetch_assoc($query)) {
			if(!in_array($row['shopid'], $shopidsArray)){
				$shopArray[] = $row['shopid'];
			}
		}
        $shopStr = implode(',',$shopArray);
    	//$sql = "select * from goods  where shopid in (".$shopStr.")  and  goods.addtime>".$endtime."  and goods.check=1 order by goods.addtime desc  limit $de,$size";
        $sql = "select * from goods  where shopid in (".$shopStr.") limit $de,$size";
    } else{
         switch ($activity_id){
				case 1:
				  $activity = "act_saohuo";
				  break;
				case 2:
				  $activity = "act_xianshizhekou";
				  break;
				case 3:
				  $activity = "act_shouxuanjingpin";
				  break;
				case 5:
				  $activity = "act_nixiangyaode";
				  break;
				default:
				  $arr = array('code' => 100, 'msg' =>'参数传入有误');
				  echo json_encode($arr);
				  exit();
			}
		$sql = "SELECT * FROM goods  WHERE  ".$activity."=2 AND goods.check=1 AND zixilie IN (".$zileimu.")   limit $de,$size";
    }
    
	$goods=$mysql->getAll($sql);

	//查找特定商品
	$sql = "SELECT goodsid FROM activity WHERE type = " . $activity_id;
	$goodsid = $mysql->getAll($sql);

	//二维数组转字符串
	$id = array();
	foreach($goodsid as $key => $value) {
		$id[$key] = $value['goodsid'];
	}
	$id = implode(',', $id);
	//end
	
	
	$sql = "SELECT * FROM goods WHERE goodsid IN (" . $id . ")";
	$teding = $mysql->getAll($sql);
	//end
	
	//合并数组
	if($teding) {
		$goods = array_merge($teding, $goods);
	}
	//end
	
	$len = count($goods);
	// var_dump($len);die;
	for($i=0;$i<$len;$i++){
		// 设置默认图
		$images = explode(',', $goods[$i]['images']);
		if ($images[$goods[$i][defaultImgOrd]] !='') {
			$defaultImg = $images[$goods[$i][defaultImgOrd]];
		}else{
			$defaultImg = $image[0];
		}
		$goods[$i]['mainimages'] = $defaultImg;
		// 设置默认图 end
		
		$goodsid =$goods[$i]['goodsid'];
		$sql2 = "select * from goodsselect where goodsid=$goodsid and number>0";
		$goods[$i]['goodsselect']=$mysql->getAll($sql2);

		$goods[$i]['number'] = $goods[$i]['xiaoliang'];
		$goods[$i]['name'] = $goods[$i]['title'];
		$goods[$i]['intro'] = $goods[$i]['summary'];
		$goods[$i]['id'] = $goods[$i]['goodsid'];
		$goods[$i]['content'] = 1;
		$goods[$i]['qq'] = $goods[$i]['qq'];
		$shopidsArray[]  = $goods[$i]['shopid'];

	}

	$arr = array('code' => 200, 'msg' =>'查询成功', 'shopids' =>implode(',',$shopidsArray) ,'goods' =>$goods);
}else{
	$arr = array('code' => 100, 'msg' =>'参数传入有误');
}
echo json_encode($arr);
?>